package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.CTDTM;
import net.sf.json.JSONObject;
import dao.CTDTD;

@WebServlet("/CTDTServlet")
public class CTDTServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CTDTD dao;
       
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public CTDTServlet() throws Exception {
        super();
        this.dao = new CTDTD();
    }

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		
		String flag = request.getParameter("flag");
		try {
			if (flag == null || "".equals(flag.trim())) {
				showCTDT(request, response);
			}else if ("add".equals(flag.trim())) {
				String ten_van_tat = request.getParameter("ten_van_tat");
				String ten_day_du = request.getParameter("ten_day_du");
				String ten_bang = request.getParameter("ten_bang");
				String so_qd = request.getParameter("so_qd");
				String ngay_qd = request.getParameter("ngay_qd");	
				String id_nganh = request.getParameter("id_nganh");
				String so_nam_tk = request.getParameter("so_nam_tk");
				String so_nam_td = request.getParameter("so_nam_td");
				addCTDT(ten_van_tat,ten_day_du,ten_bang,so_qd,ngay_qd,Integer.parseInt(id_nganh),Integer.parseInt(so_nam_tk),Integer.parseInt(so_nam_td),request,response);
			}else if ("edit".equals(flag.trim())) {
				String id_ctdt = request.getParameter("id_ctdt");
				String ten_van_tat = request.getParameter("ten_van_tat");
				String ten_day_du = request.getParameter("ten_day_du");
				String ten_bang = request.getParameter("ten_bang");
				String so_qd = request.getParameter("so_qd");
				String ngay_qd = request.getParameter("ngay_qd");	
				String id_nganh = request.getParameter("id_nganh");
				String so_nam_tk = request.getParameter("so_nam_tk");
				String so_nam_td = request.getParameter("so_nam_td");
				editCTDT(Integer.parseInt(id_ctdt),ten_van_tat,ten_day_du,ten_bang,so_qd,ngay_qd,Integer.parseInt(id_nganh),Integer.parseInt(so_nam_tk),Integer.parseInt(so_nam_td), request, response);
			}else if ("remove".equals(flag.trim())) {
				String[] id = request.getParameter("id").split(",");
				for(int i=0; i<id.length; i++){
					deleteCTDT(Integer.parseInt(id[i]), request, response);
				}
			}else if("loadTheoNganh".equals(flag.trim())){
				loadTheoNganh(request,response);
			}
			else if("loadTheoDonVi".equals(flag.trim())){
				loadTheoDonVi(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @function show CTDT
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void showCTDT(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		String search=request.getParameter("searchTen")==null?"":request.getParameter("searchTen");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		
		int offSet = (page - 1) * rows;
		int allCount = dao.searchCTDT(search).size();
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.getAlls(search, offSet, rows));
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	private void loadTheoNganh(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		String id_nganh=request.getParameter("id")==null?"0":request.getParameter("id");
		
		int offSet = (page - 1) * rows;
		int allCount = dao.countTheoNganh(Integer.parseInt(id_nganh));
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.loadTheoNganh(Integer.parseInt(id_nganh), offSet, rows));
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	private void loadTheoDonVi(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		String id_dv=request.getParameter("id")==null?"0":request.getParameter("id");
		
		int offSet = (page - 1) * rows;
		int allCount = dao.countTheoNganh(Integer.parseInt(id_dv));
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.loadTheoDonVi(Integer.parseInt(id_dv), offSet, rows));
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	/**
	 * @function add CTDT
	 * @param ten_van_tat
	 * @param ten_day_du
	 * @param ten_bang
	 * @param so_qd
	 * @param ngay_qd
	 * @param id_nganh
	 * @param so_nam_tk
	 * @param so_nam_td
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void addCTDT(String ten_van_tat, String ten_day_du, String ten_bang, String so_qd, String ngay_qd, int id_nganh, int so_nam_tk, int so_nam_td, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		CTDTM c = new CTDTM();
		c.setTen_van_tat(ten_van_tat);
		c.setTen_day_du(ten_day_du);
		c.setTen_bang(ten_bang);
		c.setSo_qd(so_qd);
		c.setNgay_qd(ngay_qd);	
		c.setId_nganh(id_nganh);
		c.setSo_nam_tk(so_nam_tk);
		c.setSo_nam_td(so_nam_td);
		if(dao.Insert(c)==true){
			map.put("success", "true");
			map.put("msg", "Thêm thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi thêm dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function edit CTDT
			if (flag == null || "".equals(flag.trim())) {
				showCTDT(request, response);
			}else if ("add".equals(flag.trim())) {
				String ten_van_tat = request.getParameter("ten_van_tat");
				String ten_day_du = request.getParameter("ten_day_du");
				String ten_bang = request.getParameter("ten_bang");
				String so_qd = request.getParameter("so_qd");
				String ngay_qd = request.getParameter("ngay_qd");	
				String id_nganh = request.getParameter("id_nganh");
				String so_nam_tk = request.getParameter("so_nam_tk");
				String so_nam_td = request.getParameter("so_nam_td");
				addCTDT(ten_van_tat,ten_day_du,ten_bang,so_qd,ngay_qd,Integer.parseInt(id_nganh),Integer.parseInt(so_nam_tk),Integer.parseInt(so_nam_td),request,response);
			}else if ("edit".equals(flag.trim())) {
				String id_ctdt = request.getParameter("id_ctdt");
				String ten_van_tat = request.getParameter("ten_van_tat");
				String ten_day_du = request.getParameter("ten_day_du");
				String ten_bang = request.getParameter("ten_bang");
				String so_qd = request.getParameter("so_qd");
				String ngay_qd = request.getParameter("ngay_qd");	
				String id_nganh = request.getParameter("id_nganh");
				String so_nam_tk = request.getParameter("so_nam_tk");
				String so_nam_td = request.getParameter("so_nam_td");
				editCTDT(Integer.parseInt(id_ctdt),ten_van_tat,ten_day_du,ten_bang,so_qd,ngay_qd,Integer.parseInt(id_nganh),Integer.parseInt(so_nam_tk),Integer.parseInt(so_nam_td), request, response);
			}else if ("remove".equals(flag.trim())) {
				String[] id = request.getParameter("id").split(",");
				for(int i=0; i<id.length; i++){
					deleteCTDT(Integer.parseInt(id[i]), request, response);
				}
			}
		
	 * @param id_ctdt
	 * @param ten_van_tat
	 * @param ten_day_du
	 * @param ten_bang
	 * @param so_qd
	 * @param ngay_qd
	 * @param id_nganh
	 * @param so_nam_tk
	 * @param so_nam_td
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void editCTDT(int id_ctdt, String ten_van_tat, String ten_day_du, String ten_bang, String so_qd, String ngay_qd, int id_nganh, int so_nam_tk, int so_nam_td, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		CTDTM c = new CTDTM();
		c.setId_ctdt(id_ctdt);
		c.setTen_van_tat(ten_van_tat);
		c.setTen_day_du(ten_day_du);
		c.setTen_bang(ten_bang);
		c.setSo_qd(so_qd);
		c.setNgay_qd(ngay_qd);	
		c.setId_nganh(id_nganh);
		c.setSo_nam_tk(so_nam_tk);
		c.setSo_nam_td(so_nam_td);
		if(dao.Update(c)==true){
			map.put("success", "true");
			map.put("msg", "Sửa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi sửa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function delete CTDT
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void deleteCTDT(int id, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dao.Delete(id)==true){
			map.put("success", "true");
			map.put("msg", "Xóa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi xóa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
}